package com.aaron.crowd.mvc.handler;

import com.aaron.crowd.util.ResultEntity;
import com.aaron.crowd.entity.Admin;
import com.aaron.crowd.entity.ListArray;
import com.aaron.crowd.entity.Student;
import com.aaron.crowd.service.api.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @BelongsProject: atcrowdfuncding
 * @BelongsPackage: com.aaron.crowd.mvc.handler
 * @author: Andy
 * @date: 2021.3.24 14:44
 * @Description: 测试的Handler
 * @since JDK 1.8
 */
@Controller
public class TestHandler {
    @Autowired
    private AdminService adminService;
    private Logger logger = LoggerFactory.getLogger(TestHandler.class);

    @RequestMapping("test/ssm")
    public String testSsm(ModelMap modelMap, HttpServletRequest request){
        List<Admin> all = adminService.getAll();
        modelMap.addAttribute("adminList", all);
         int i = 10 / 0;
        //String a = null;
        //System.out.println(a.length());
        return "target";
    }

    @ResponseBody
    @RequestMapping("send/array/one")
    public String textReceiveArrayOne(@RequestParam("array[]") List<Integer> array){
        for (Integer number : array){
            System.out.println("number="+number);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("send/array/two")
    public String textReceiveArrayTwo(ListArray listArray){
        List<Integer> array = listArray.getArray();
        for (Integer number : array){
            System.out.println("number="+number);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("send/array/three")
    public String testReceiveArray(@RequestBody List<Integer> array) {
        array.forEach(System.out::println);
        return "success";
    }

    @ResponseBody
    @RequestMapping("send/compose/object")
    public ResultEntity<Student> testReceiveComposeObject(@RequestBody Student student,
                                                          HttpServletRequest request){
        // 将查询到的Student的对象封装到ResultEntity对象中返回
        String a = null;
        System.out.println(a.length());
        return ResultEntity.successWithData(student);
    }

}
